﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data;

namespace Tailord.Reports
{
    public partial class xrSubFatura2016 : DevExpress.XtraReports.UI.XtraReport
    {
        public xrSubFatura2016()
        {
            InitializeComponent();
        }

        DataSet ds;
        DataTable dtIrsaliyeLines;
        public void fillTable(DataTable dt)
        {
            ds = new DataSet();
            dtIrsaliyeLines = new DataTable("dtIrsaliyeLines");
            ds.Tables.Add(dtIrsaliyeLines);
            dtIrsaliyeLines.Merge(dt);

            this.DataMember = dtIrsaliyeLines.TableName;
            this.DataSource = ds;
        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            this.DataMember = dtIrsaliyeLines.TableName;
            this.DataSource = ds;

            foreach (XRTableCell cell in trIrsaliyeLines)
            {
                cell.DataBindings.Add("Text", null, "dtIrsaliyeLines." + cell.Name);
            }
        }

        private void ReSize(object sender)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 8f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.Width / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }

        private void tcKod_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

    }
}
